package dev.blogmoon.cn.admin.config;

import cn.dev33.satoken.stp.StpUtil;
import dev.blogmoon.cn.common.context.Context;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


import java.io.IOException;

@Component

public class WebFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (StpUtil.isLogin()) {
            Context.get().setBizAcctId(StpUtil.getLoginIdAsLong());
        }
        if (StringUtils.isNotBlank(request.getHeader("versionCode"))) {
            Context.get().setVersionCode(Integer.valueOf(request.getHeader("versionCode")));
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
